import { Address, TonClient4 , TonClient } from "@ton/ton";



export async function fetchContractData(contractAddress: string,network: string | undefined): Promise<any> {
    const client1 = new TonClient({
        endpoint: "https://testnet.toncenter.com/api/v2/jsonRPC",
    });
    const CHAIN = {
        MAINNET : "-239",
        TESTNET : "-3"
    }

    const client = new TonClient4({ endpoint: network === CHAIN.MAINNET
        ? "https://mainnet-v4.tonhubapi.com"
        : "https://sandbox-v4.tonhubapi.com" });
    // const client = new TonClient4({ endpoint: "https://mainnet-v4.tonhubapi.com" });
    
    // 获取最新的区块信息
    const lastBlock = await client.getLastBlock();
    const blockNumber = lastBlock.last.seqno; // 最新区块高度

    let methodName = ""
    try {
        
        // const get_contract_info = await client.runMethod(blockNumber, Address.parse(contractAddress), "xxx");
        const response = await client.runMethod(blockNumber, Address.parse(contractAddress), "get_total_supply");
        // const get_owner = await client.runMethod(blockNumber, Address.parse(contractAddress), "get_owner");
        // const get_balance = await client.runMethod(blockNumber, Address.parse(contractAddress), "get_balance");
        // const get_symbol = await client.runMethod(blockNumber, Address.parse(contractAddress), "get_symbol");
        // const get_decimals = await client.runMethod(blockNumber, Address.parse(contractAddress), "get_decimals");
        // const get_wallet_address = await client.runMethod(blockNumber, Address.parse(contractAddress), "get_wallet_address");
        // const get_content = await client.runMethod(blockNumber, Address.parse(contractAddress), "get_content");
        
        // console.log("get_contract_info==")
        // console.log(get_contract_info)
        // console.log(get_owner)
        // console.log(get_balance)
        // console.log(get_symbol)
        // console.log(get_decimals)
        // console.log(get_wallet_address)
        // console.log(get_content)
        const result = await client.runMethod(blockNumber, Address.parse(contractAddress), "get_counter");

    console.log("📊 计数器当前值:", result.result[0]);
        
        
        console.log("代币总供应量:", (response.result[0] as any)?.value.toString());
        console.log(`✅ 方法 ${methodName} 返回值:`, response.result);

        
    } catch (error) {
        console.log(`❌ 方法 ${methodName} 不存在`);
    }
    

    // 获取合约数据（需要地址和区块高度）
    const contract = await client.getAccount(blockNumber, Address.parse(contractAddress));

    console.log('client----------')
    console.log(client)
    console.log('contract----------')
    console.log(contract)
    console.log('lastBlock-blockNumber----------')
    console.log(lastBlock,blockNumber)
    
    // const methodNames = contract.account.methods || [];
    // console.log("📜 合约可用方法:", methodNames);

    if (!contract.account.state) {
        throw new Error("合约未部署或无数据");
    }

    return contract.account.state;



}
